package org.hishen.example.service.admin.mall;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hishen.example.entity.mall.Goods;
import org.hishenframework.core.hibernate.EasyOperater;
import org.hishenframework.data.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class GoodsService {

	@Resource
	private SessionFactory factory;
	
	/** 查询 */
	public void list2(Page page, String goodsname, int beginPrice, int endPrice) {
		Session session = factory.getCurrentSession();
		Query query = session.createQuery(
				"select count(*) from Goods where goodsname = ? and price between ? and ?");
		query.setString(0, goodsname);
		query.setInteger(1, beginPrice);
		query.setInteger(2, endPrice);
		Long count = (Long) query.uniqueResult();
		page.setTotalCount(count.intValue());
		query = session.createQuery(
				"from Goods where goodsname = ? and price between ? and ? order by insertTime");
		query.setString(0, goodsname);
		query.setInteger(1, beginPrice);
		query.setInteger(2, endPrice);
		page.setResult(query.list());
	}
	
	public void save2(Goods goods) {
		Session session = factory.getCurrentSession();
		session.save(goods);
	}
	
	@Resource
	private EasyOperater op;

	public void list(Page page, String goodsname, int beginPrice, int endPrice) {
		String hql = "from Goods where goodsname = ? and price between ? and ? order by insertTime";
		op.list(page, hql, goodsname, beginPrice, endPrice);
	}
	/**
	 * 保存
	 * @param goods
	 */
	public void save(Goods goods) {
		op.save(goods);
	}
	
	/**
	 * 修改
	 * @param goods
	 */
	public void update(Goods goods) {
		op.update(goods);
	}
	
	/**
	 * 删除
	 * @param goods
	 */
	public void delete(Goods... goods) {
		for (Goods o : goods) {
			op.delete(o);
		}
	}
	
	/**
	 * 通过主键获取一个Goods
	 * @param id
	 */
	public Goods get(int id) {
		return op.load(Goods.class, id);
	}
	
	/**
	 * 列表
	 * @param page
	 */
	public void list(Page page) {
		op.list(page, "from Goods");
	}
}
